package com.polaris.live.widget

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.databinding.WidgetPreviewEntryBinding
import com.polaris.live.utils.file.FileUtils
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.avatar.FrameImageUtils
import org.libpag.PAGFile
import org.libpag.PAGScaleMode
import java.io.File


/**
 * PreViewEntryScreenItemView 预览直播间进场飘屏
 * @author Created by 天晴 on 2024/1/29 14:30
 * @since 1.0.0
 **/
@SuppressLint("ResourceType")
class PreViewEntryScreenItemView(
    context: Context,
    attrs: AttributeSet? = null,
) : ConstraintLayout(context, attrs) {

    private val mBinding = WidgetPreviewEntryBinding.inflate(LayoutInflater.from(context), this, true)

    private lateinit var mPath: String
    private var mCombinedAnimatorSet: AnimatorSet? = null

    //结束回调
    private var mOnEntryScreenAnimationEnd: ((userId: Long) -> Unit)? = null

    init {
        visibility = INVISIBLE
        //目前测试pag会反转过来的
        mBinding.previewPagView.scaleX = if (AppCodeUtils.isAR()) {
            -1f
        } else {
            1f
        }
    }

    fun setData(
        path: String,
    ) {
        mPath = path
        val user = UserManager.getUser()
        mBinding.apply {
            val lp = root.layoutParams as LayoutParams
            if (AppCodeUtils.isAR()) {
                lp.rightMargin = 320.autoDp
            } else {
                lp.leftMargin = 320.autoDp
            }
            lp.width = 320.autoDp
            lp.height = 70.autoDp
            root.layoutParams = lp
            if (mPath.endsWith(PagConst.PAG)) {
                mBinding.previewPagView.visibility = View.VISIBLE
                mBinding.image.visibility = View.GONE
                val path = FileUtils.getFilePath(FileUtils.getPagFile(), mPath)
                if (File(path).exists()) {
                    val pagFile = PAGFile.Load(path)
                    mBinding.previewPagView.apply {
                        setScaleMode(PAGScaleMode.Zoom)
                        composition = pagFile
                        setRepeatCount(-1)
                        play()
                    }
                }
            } else {
                mBinding.previewPagView.visibility = View.GONE
                mBinding.image.visibility = View.VISIBLE
                Glide.with(context)
                    .load(ImageUtils.imgPath(mPath))
                    .centerCrop()
                    .into(image)
            }

            avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(user.decoration?.avatarFrame))
            avatar.setImageUrl(user.avatar)
            lvLevel.setData(user.sex, user.account?.richs, user.account?.charms)
            userName.text = user.nickname
        }

    }


    //礼物动画分发展示
    fun startAnimation() {
        if (AppCodeUtils.isAR()) {
            startRtLAnimation()
            return
        }
        post {
            visibility = VISIBLE
            val animatorCenterX =
                ObjectAnimator.ofFloat(this, "translationX", 320.autoDp.toFloat(), 0.autoDp.toFloat())
            val animatorAlpha = ObjectAnimator.ofFloat(this, "alpha", 0.2f, 1f)
            animatorCenterX.duration = 500
            animatorAlpha.duration = 500
            val animatorSet = AnimatorSet()
            animatorSet.playTogether(animatorCenterX, animatorAlpha)

            val animatorCenterX2 =
                ObjectAnimator.ofFloat(this, "translationX", 0.autoDp.toFloat(), 0f)
            animatorCenterX2.duration = 3000

            val animatorCenterX3 =
                ObjectAnimator.ofFloat(this, "translationX", 0f, -320.autoDp.toFloat())
            animatorCenterX3.duration = 500

            // 将两组动画结合起来
            mCombinedAnimatorSet = AnimatorSet()
            mCombinedAnimatorSet?.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    visibility = GONE
                    mOnEntryScreenAnimationEnd?.invoke(UserManager.getUserId())
                    mCombinedAnimatorSet = null
                }
            })
            mCombinedAnimatorSet?.playSequentially(animatorSet, animatorCenterX2, animatorCenterX3)
            mCombinedAnimatorSet?.start()
        }

    }
    //礼物动画分发展示RtL
    private fun startRtLAnimation() {
        post {
            visibility = VISIBLE
            val animatorCenterX =
                ObjectAnimator.ofFloat(this, "translationX", -320.autoDp.toFloat(), 0f)
            val animatorAlpha = ObjectAnimator.ofFloat(this, "alpha", 0.2f, 1f)
            animatorCenterX.duration = 500
            animatorAlpha.duration = 500
            val animatorSet = AnimatorSet()
            animatorSet.playTogether(animatorCenterX, animatorAlpha)

            val animatorCenterX2 =
                ObjectAnimator.ofFloat(this, "translationX", 0f, 0f)

            animatorCenterX2.duration = 3000

            val animatorCenterX3 =
                ObjectAnimator.ofFloat(this, "translationX", 0f, 320.autoDp.toFloat())
            animatorCenterX3.duration = 500

            // 将两组动画结合起来
            mCombinedAnimatorSet = AnimatorSet()
            mCombinedAnimatorSet?.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    visibility = GONE
                    mOnEntryScreenAnimationEnd?.invoke(UserManager.getUserId())
                    mCombinedAnimatorSet = null
                }
            })
            mCombinedAnimatorSet?.playSequentially(animatorSet, animatorCenterX2, animatorCenterX3)
            mCombinedAnimatorSet?.start()
        }
    }

    fun cancelAnimation() {
        mCombinedAnimatorSet?.cancel()
        mCombinedAnimatorSet = null
    }


    fun setOnEntryScreenAnimationEnd(click: (userId: Long) -> Unit) {
        mOnEntryScreenAnimationEnd = click
    }

}